﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YYTmEditer.MtObject;
using yyycommon;

namespace YYTmEditer
{
    /// <summary>
    /// CardLibController.xaml 的交互逻辑
    /// </summary>
    public partial class CardLibController : UserControl
    {
        public CardLibController()
        {
            InitializeComponent();
            this.Loaded += this.OnLoaded;
            this.comboBox.SelectionChanged += this.OnGroupChange;
        }

        private List<RespObj> allList;
        private void OnGroupChange(object sender, SelectionChangedEventArgs e)
        {
            if (this.allList == null) {
                return;
            }
            if (this.comboBox.SelectedIndex == 0)
            {
                this.listBox.ItemsSource = null;
                this.listBox.ItemsSource = allList;
            }
            else {
                string gp = (this.comboBox.SelectedItem as ListBoxItem).Content.ToString();
                List<RespObj> list = new List<RespObj>();
                for (int i = 0; i < allList.Count; i++) {
                    if (gp.Equals(allList[i].mt_group)) {
                        list.Add(allList[i]);
                    }
                }
                this.listBox.ItemsSource = null;
                this.listBox.ItemsSource = list;
            }
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var list = EditObject.CardList();
            this.listBox.ItemsSource = list;
            allList = list;
            listBox.SelectionChanged += ListBox_SelectionChanged;
            this.button1.Click += Button1_Click;
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            var cardObj = this.listBox.SelectedItem as RespObj;
            if (cardObj != null)
            {
                var be = new BlessingEditer();
                var eo = new EditObject(cardObj.mt_name+"-"+ cardObj.mt_desc, cardObj.mt_id, "card");
                be.SetBlessing(eo);
                be.ShowDialog();
                if (!string.IsNullOrEmpty(eo.name))
                {
                    cardObj.mt_name = eo.name;
                    cardObj.mt_desc = eo.desc;
                    cardObj.mt_group = eo.group;
                }
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cardObj = this.listBox.SelectedItem as RespObj;
            if (cardObj != null)
            {
                this.button1.IsEnabled = cardObj.mt_lock == 0;
            }
        }
            private void button_Click(object sender, RoutedEventArgs e)
        {
            var cardObj = this.listBox.SelectedItem as RespObj;
            if (cardObj != null) {
                var clist = GameObject.Instance.GetCards();
                var card = new Card();
                card.cardDataID = cardObj.mt_id;
                clist.Add(card);
                GameObject.Instance.SetCards(clist);
                MessageBox.Show("添加成功");
            }
        }
    }
}
